#include "loginwidget.h"
#include "ui_loginwidget.h"

LoginWidget::LoginWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoginWidget)
{
    this->file1 = new file;
    ui->setupUi(this);
    this->setWindowTitle("SCUT教务系统");

    ui->loginPassword->setEchoMode(QLineEdit::Password);

    //确认登陆
    connect(ui->loginButton,SIGNAL(clicked(bool)),this,SLOT(Login()));
}

LoginWidget::~LoginWidget()
{
    delete ui;
}

void LoginWidget::on_exitButton_clicked()
{
    QApplication::exit();
}



//判断账号密码是否正确
bool LoginWidget::check(QString id,QString password)
{
    for(int i=0;i<this->infoLine.length();i++)
    {
        QString line=infoLine.at(i);
        line=line.trimmed();
        QStringList linesplit=line.split(",");
        if(id==linesplit.at(1) && password==linesplit.at(2))
        {
            return true;
        }
    }
        return false;
}

//登录按钮
void LoginWidget::on_loginButton_clicked()
{
    QString id=this->ui->loginId->text();
    QString password=this->ui->loginPassword->text();
    if(this->ui->studentButton->isChecked())
    {
        this->file1->readFile("student.csv",infoLine);
        for(int i=0;i<this->infoLine.length();i++)
        {
            QString line=infoLine.at(i);
            line=line.trimmed();
            QStringList linesplit=line.split(",");
        }
        if(this->check(id,password))
        {
            this->studentwidget = new StudentWidget;
            this->studentwidget->getLoginId(id);
            this->studentwidget->show();
            this->studentwidget->setStuScore();
            this->studentwidget->setStuObject();
            this->close();
        }
        else
        {
            QMessageBox::critical(this,"错误！","用户名或密码错误！","确定");
        }
    }
    else if(this->ui->teacherButton->isChecked())
    {
        this->file1->readFile("teacher.csv",infoLine);
        if(this->check(id,password))
        {
            this->teacherwidget = new TeacherWidget;
            this->teacherwidget->getLoginId(id);
            this->teacherwidget->show();
            this->teacherwidget->setTeaObject();
            this->close();
        }
        else
        {
            QMessageBox::critical(this,"错误！","用户名或密码错误！","确定");
        }
    }
    else if(this->ui->managerButton->isChecked())
    {
        this->file1->readFile("manager.csv",infoLine);
        if(this->check(id,password))
        {
            this->managerwidget = new ManagerWidget;
            this->managerwidget->getLoginId(id);
            this->managerwidget->show();
            this->managerwidget->setManageObject();
            this->close();
        }
        else
        {
            QMessageBox::critical(this,"错误！","用户名或密码错误！","确定");
        }
    }
    else
    {
        QMessageBox::critical(this,"错误！","请选择类别！","确定");
    }
}

void LoginWidget::on_modifyButton_clicked()
{
    if(this->ui->studentButton->isChecked())
    {
        this->modifywidget = new ModifyWidget;
        this->modifywidget->show();
        this->close();
    }
    else if(this->ui->teacherButton->isChecked())
    {

    }
    else if(this->ui->managerButton->isChecked())
    {

    }
    else
    {
        QMessageBox::critical(this,"错误！","请选择类别！","确定");
    }


}
